import logging, time, os


class Logger:
    levels = ["CRITICAL", "ERROR", "WARN", "WARNING", "INFO", "DEBUG"]

    def __init__(self, package, console_log_level="info", log_dir="d:/log"):
        # log日志
        log_path = log_dir + "/" + package + "/"
        if not os.path.exists(log_path):
            os.makedirs(log_path)
        handler = logging.FileHandler(log_path + package + '-%s.log' % time.strftime('%Y-%m-%d'), encoding='u8')
        formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
        handler.setFormatter(formatter)
        root_logger = logging.getLogger()
        root_logger.addHandler(handler)
        root_logger.setLevel(logging.DEBUG)
        # 屏幕输出
        console = logging.StreamHandler()
        console_log_level = console_log_level.upper()
        assert console_log_level in self.levels, "check the log level"
        console.setLevel(logging.getLevelName(console_log_level))
        formatter = logging.Formatter('%(levelname)-8s %(message)s')
        console.setFormatter(formatter)
        root_logger.addHandler(console)
        # 设置logger等级
        # logging.getLogger("requests").setLevel(logging.WARNING)
        # logging.getLogger('selenium').setLevel(logging.WARNING)
        self.__logging = logging
        self.info = logging.info
        self.debug = logging.debug
        self.error = logging.error
        self.warning = logging.warning
        self.critical = logging.critical

    def set_logger_level(self, obj, level):
        assert level in self.levels, "check the log level"
        self.__logging.getLogger(obj).setLevel(logging.getLevelName(level))


if __name__ == '__main__':
    print(os.getcwd())
